package com.pf.service.impl;

import com.pf.entity.Child;
import com.pf.entity.Parent;
import com.pf.mapper.ChildMapper;
import com.pf.mapper.ParentMapper;
import com.pf.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2022/6/27 23:00
 */
@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ParentMapper parentMapper;
    @Autowired
    private ChildMapper childMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void test() {

        threadPoolExecutor.execute(() -> {
            try{
                System.out.println(">>>>>> test...");
                Thread.sleep(20000);
            }catch (Exception e){
                e.printStackTrace();
            }
        });


//        Parent parent = Parent.builder().parentName("parent").build();
//        parentMapper.insert(parent);
//        System.out.println("parentId=" + parent.getParentId());
//        Child child = Child.builder().childName("child").parentId(parent.getParentId()).build();
//        childMapper.insert(child);
//        int i = 1 / 0;
//        threadPoolExecutor.execute(() -> {
//            try{
//                Child child = Child.builder().childName("child").parentId(parent.getParentId()).build();
//                childMapper.insert(child);
//                int i = 1 / 0;
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        });
    }
}
